import ajax from './axios'
import { message } from 'antd'
import jsonp from 'jsonp'

export const reqLogin = (username,password)=> ajax('/login', {username, password}, 'POST')

export const reqUserAdd = (user)=> ajax('/manage/user/add',user, 'POST')

// 获取一级或某个二级分类列表
export const reqCategory = (parentId)=> ajax('/manage/category/list',{parentId})
// 添加分类
export const addCategory = ({parentId,categoryName})=> ajax('manage/category/add',{parentId,categoryName},'POST')

// 更新分类名称
export const updateCategory = ({categoryId,categoryName})=> ajax('manage/category/update',{categoryId,categoryName},'POST') 

// 获取商品分页列表

export const reqProductList =(pageNum,pageSize)=> ajax('/manage/product/list',{pageNum,pageSize})
export const reqSearchProducts = ({pageNum, pageSize, searchName, searchType}) => ajax('/manage/product/search', {
    pageNum,
    pageSize,
    [searchType]: searchName,
  })


// 根据id获取分类

export const reqIdCategorys=({categoryId })=>ajax('/manage/category/info',{categoryId })

// 对商品进行上架/下架处理
// export const reqUpdateBtnStatus = ({productId,status})=>ajax('/product/updateStatus',{productId,status},'POST')

export const reqUpdateBtnStatus = (productId, status) => ajax('/manage/product/updateStatus', { productId, status }, 'POST')

// 上传图片

export const uploadImages=(image)=> ajax('/manage/img/upload',{image},'POST')

// 删除图片
export const deleteImgs = (name) => ajax('/manage/img/delete',{name}, 'POST')
// 添加或者更新商品
export const reqAddOrUpdate = (product) => ajax('/manage/product/' + (product._id ?'update':'add'  ),product, 'POST')

// 获取角色列表
// export const roleData = ()=>ajax('/manage/role/list')
export const roleData = () => ajax('/manage/role/list')

// 添加角色

export const addRole = (roleName)=>ajax('/manage/role/add',{roleName},'POST')


// 更新角色

export const reqUpdateRole = (role)=> ajax('/manage/role/update',role,'POST')

// 获取用户角色列表

export const reqUserList = ()=>ajax('/manage/user/list')

// 添加用户

export const reqAddUser = (user)=>ajax('/manage/user/add',user,'POST')

// 删除用户数据
export const reqDeleteUser = (userId)=>ajax('/manage/user/delete',{userId},'POST')
// 更新用户
export const reqUpdateUser = (user)=>ajax('/manage/user/update',user,'POST')




export const reqWeather =(city)=>{
    return new Promise((resolve,reject)=>{
        const url =`http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`;
        jsonp(url,{},(err,data)=>{
           if(!err&&data.status==='success'){
               const {dayPictureUrl,weather} = data.results[0].weather_data[0]
               resolve({dayPictureUrl,weather})
           }
            else{
                message.error('天气接口获取失败')
            }
        })

    })
}
